//
//  SEGActivityTriggerLotteryViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/17.
//  Copyright © 2017年 陈区. All rights reserved.
//

// ViewController
#import "SEGActivityTriggerLotteryViewController.h"
#import "SEGWinRecordDetailViewController.h"
#import "SEGWinRecordListViewController.h"
// View
#import "SEGActivityLotteryRotateView.h"
#import "SEGActivityLotteryBreakEggsView.h"
#import "SEGActivityWinRecordView.h"
#import "SEGAlerViewHUD.h"
// Model
#import <SEGModel/SEGActivityLotteryDetailModel.h>
#import <SEGModel/SEGActivityLotteryResultModel.h>
// ViewModel
#import <SEGViewModel/SEGActivityDetailViewModel.h>

@interface SEGActivityTriggerLotteryViewController ()

/// 主视图
@property (nonatomic, strong) UIScrollView *scrollView;
/// 文字颜色
@property (nonatomic, strong) UIColor *textColor;
/// 头部抽奖视图
@property (nonatomic, strong) UIImageView *topImageView;
/// 中奖记录
@property (nonatomic, strong) SEGAdjustableButton *winRecordButton;
/// 最新动态
@property (nonatomic, strong) UILabel *newsLabel;
/// 最新动态左侧图片
@property (nonatomic, strong) UIImageView *newsLeftImage;
/// 最新动态右侧图片
@property (nonatomic, strong) UIImageView *newsRightImage;
/// 底部标签
@property (nonatomic, strong) UILabel *bottomLabel;
/// 调用抽奖结果是否成功
@property (nonatomic, assign) BOOL isLotterySucceed;
/// 调用抽奖结果错误信息
@property (nonatomic, copy) NSString *lotteryErrorMessage;
/// 点击的金蛋序号
@property (nonatomic, assign) NSInteger eggIndex;
/// 标识第一次加载（第一次加载的时候才统计浏览）
@property (nonatomic, assign) __block BOOL isFirstLoad;
/// 抽奖页面
@property (nonatomic, strong) SEGActivityWinRecordView *winRecordView;
/// ViewModel
@property (nonatomic, strong) SEGActivityDetailViewModel *detailViewModel;
/// 抽奖详情Model
@property (nonatomic, strong) SEGActivityLotteryDetailModel *lotteryDetailModel;
/// 抽奖结果Model
@property (nonatomic, strong) SEGActivityLotteryResultModel *lotteryResultModel;
/// 缓存砸金蛋抽奖视图
@property (nonatomic, assign) SEGActivityLotteryBreakEggsView *tempEggView;

@end

@implementation SEGActivityTriggerLotteryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColor_bg;
    self.isFirstLoad = YES;
    
    // 初始化数据
    self.detailViewModel.actId = ValueToString(self.lotteryId);
    self.detailViewModel.lotteryId = ValueToString(self.lotteryId);
    
    [self showCustomNavigationBackButton];
    [self seg_requestLottery];

    // 监听进入前台时重新开始砸金蛋的动画
    WeakSelf;
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationWillEnterForegroundNotification object:nil] subscribeNext:^(NSNotification * notification) {
        if (weakSelf.tempEggView) {
            [weakSelf.tempEggView startHummerMoveAnimation];
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
    // 需要重新开始砸金蛋的动画
    if (self.tempEggView) {
        [self.tempEggView startHummerMoveAnimation];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)seg_requestLottery {
    [SVProgressHUD show];
    //请求活动详情
    self.detailViewModel.isTriggerLottery = YES;
    [self.detailViewModel.requestLotteryDetailSignal subscribeNext:^(id  _Nullable x) {
        [SVProgressHUD dismiss];
        self.lotteryDetailModel = x;
        SEGActivityLotteryType type = [self.lotteryDetailModel.lotteryType intValue];
        switch (type) {
            case SEGActivityLottery_BreakEggs://砸金蛋
            {
                [self seg_showBreakEggsView];
                break;
            }
            case SEGActivityLottery_Turntable://大转盘
            {
                [self seg_showTurnTableView];
                break;
            }
            case SEGActivityLottery_RedWallet://红包
            {
                [self seg_showRedWalletView];
                break;
            }
        }
    } error:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
        if (error.code == 10) {
            [self seg_removeCurrentLotteryInfoInLocalArray];
        }
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription];
    }];
}

- (void)seg_showBreakEggsView
{
    self.title = @"砸金蛋";
    self.scrollView.backgroundColor = [UIColor colorWithRed:210 / 255.0 green:59 / 255.0 blue:48 / 255.0 alpha:1];
    self.textColor = UIColor.whiteColor;

    self.topImageView.frame = CGRectMake(0, 0, LYScreenWidth(), 375 * LYScreenScaleWidth());
    SEGActivityLotteryBreakEggsView *view = [[SEGActivityLotteryBreakEggsView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 375 * LYScreenScaleWidth()) finished:^(NSInteger index) {
        self.eggIndex = index;
        [self seg_beginLottery];
    }];
    view.tag = 1003;
    [self.topImageView addSubview:view];
    [view startHummerMoveAnimation];
    [self seg_setNewsViewWithImageIndex:3];
    self.tempEggView = view;
}

- (void)seg_showTurnTableView
{
    self.title = @"大转盘";
    self.scrollView.backgroundColor = [UIColor colorWithRed:79 / 255.0 green:211 / 255.0 blue:185 / 255.0 alpha:1];
    self.textColor = UIColor.whiteColor;
    [self.view addSubview:self.scrollView];
    
    self.topImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity01_bg")];
    self.topImageView.frame = CGRectMake(0, 0, LYScreenWidth(), 405 * LYScreenScaleWidth());
    
    /*4.6.0 添加谢谢参与的缺省图链接*/
    SEGActivityLotteryRotateView *view = [[SEGActivityLotteryRotateView alloc] initWithFrame:CGRectMake((LYScreenWidth() - 340 * LYScreenScaleWidth()) / 2.0, 65, 340 * LYScreenScaleWidth(), 340 * LYScreenScaleWidth()) dataSource:self.lotteryDetailModel.prizes placehoder:self.lotteryDetailModel.notLotteryPic finished:^(SEGActivityLotteryPrizeModel *result, BOOL isClickButton) {
        if (isClickButton) {//点击 请求抽奖接口
            [self seg_beginLottery];
        }else {//展示抽奖结果弹窗
            [self seg_showLotteryResult];
        }
    }];
    
    
    view.tag = 1001;
    [self.topImageView addSubview:view];
    
    [self seg_setNewsViewWithImageIndex:1];
}

- (void)seg_showRedWalletView
{
    self.title = @"红包";
    self.scrollView.backgroundColor = [UIColor colorWithRed:243 / 255.0 green:57 / 255.0 blue:58 / 255.0 alpha:1];
    self.textColor = [UIColor colorWithRed:251 / 255.0 green:211 / 255.0 blue:130 / 255.0 alpha:1];
    
    self.topImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity02_bg")];
    self.topImageView.frame = CGRectMake(0, 0, LYScreenWidth(), 375 * LYScreenScaleWidth());

    //添加开红包操作
    UIButton *openButton = [[UIButton alloc] initWithFrame:CGRectMake((LYScreenWidth() - 85.0) / 2.0, 140, 85.0, 85.0)];
    [openButton addTarget:self action:@selector(seg_beginLottery) forControlEvents:UIControlEventTouchUpInside];
    [self.topImageView addSubview:openButton];
    
    [self seg_setNewsViewWithImageIndex:2];
}

- (void)seg_setNewsViewWithImageIndex:(NSUInteger)index
{
    if (index == 2) {
        [self.winRecordButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity02_gift")] forState:UIControlStateNormal];
        self.bottomLabel.textColor = [UIColor colorWithRed:251 / 255.0 green:211 / 255.0 blue:130 / 255.0 alpha:0.5];
    }else {
        [self.winRecordButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity01_gift")] forState:UIControlStateNormal];
        self.bottomLabel.textColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:0.5];
    }
    self.winRecordButton.frame = CGRectMake((LYScreenWidth() - 125) / 2.0, CGRectGetMaxY(self.topImageView.frame) + 29, 125, 15);
    
    //最新动态
    self.newsLabel.frame =  CGRectMake((LYScreenWidth() - 70) / 2.0, CGRectGetMaxY(self.winRecordButton.frame) + 60, 70, 15);
    NSString *imageName = [NSString stringWithFormat:@"img_activity0%lu_lineleft",(unsigned long)index];
    UIImage *leftImage = [UIImage imageWithContentsOfFile:SEGPicName(imageName)];
    self.newsLeftImage.image = leftImage;
    self.newsLeftImage.frame = CGRectMake(CGRectGetMinX(self.newsLabel.frame) - leftImage.size.width , CGRectGetMidY(self.newsLabel.frame) - leftImage.size.height / 2.0, leftImage.size.width, leftImage.size.height);
    NSString *imageName2 = [NSString stringWithFormat:@"img_activity0%lu_lineright",(unsigned long)index];
    UIImage *rightImage = [UIImage imageWithContentsOfFile:SEGPicName(imageName2)];
    self.newsRightImage.image = rightImage;
    self.newsRightImage.frame = CGRectMake(CGRectGetMaxX(self.newsLabel.frame) , CGRectGetMidY(self.newsLabel.frame) - rightImage.size.height / 2.0, rightImage.size.width, rightImage.size.height);
    
    if([self.lotteryDetailModel.lotteryUserNum intValue] <= 0) {
        //还没有人参加抽奖 缺省1界面
        UIImage *image;
        if (index == 2) {//红包显示黄色图片
            image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity_nonew_y")];
        }else {
            image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity_nonew_w")];
        }
        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
        [self.scrollView addSubview:imgView];
        imgView.frame = CGRectMake((LYScreenWidth() - 120) / 2.0, CGRectGetMaxY(self.newsLabel.frame) + 30, 120, 80);
        
        UILabel *label = [[UILabel alloc] init];
        label.font = kFont_13;
        label.textColor = self.textColor;
        label.text = @"还在等什么！赶紧去抽奖呀！";
        label.textAlignment = NSTextAlignmentCenter;
        [self.scrollView addSubview:label];
        label.frame = CGRectMake(0, CGRectGetMaxY(imgView.frame) + 13, LYScreenWidth(), 15);
        
        self.bottomLabel.frame = CGRectMake((LYScreenWidth() -  CGRectGetWidth(self.bottomLabel.bounds) ) / 2.0, CGRectGetMaxY(label.frame) + 88, CGRectGetWidth(self.bottomLabel.bounds), 15);
    }else {
        UILabel *numLabel = [[UILabel alloc] init];
        numLabel.font = kFont_13;
        numLabel.textColor = self.textColor;
        NSString *string = [NSString stringWithFormat:@"已有 %@ 名邻居参与了抽奖！",self.lotteryDetailModel.lotteryUserNum];
        NSMutableAttributedString *mutableStr = [[NSMutableAttributedString alloc] initWithString:string];
        NSRange strRange = [string rangeOfString:self.lotteryDetailModel.lotteryUserNum];
        [mutableStr addAttribute:NSFontAttributeName value:kFont_16 range:strRange];
        numLabel.attributedText = mutableStr;
        numLabel.textAlignment = NSTextAlignmentCenter;
        [self.scrollView addSubview:numLabel];
        numLabel.frame = CGRectMake(0, CGRectGetMaxY(self.newsLabel.frame) + 10, LYScreenWidth(), 20);
        
        UIView *recordBgView = [[UIView alloc] init];
        recordBgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.05];
        recordBgView.layer.masksToBounds = YES;
        recordBgView.layer.cornerRadius = 5;
        [self.scrollView addSubview:recordBgView];
        
        if(self.lotteryDetailModel.lotteryUsers.count <= 0) {
            //还没有人中奖 缺省2界面
            recordBgView.frame = CGRectMake(15, CGRectGetMaxY(numLabel.frame) + 28, LYScreenWidth() - 30, 162.0);
            
            UIImage *image;
            if (index == 2) {//红包显示黄色图片
                image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity_nowin_y")];
            }else {
                image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_activity_nowin_w")];
            }
            UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
            [recordBgView addSubview:imgView];
            imgView.frame = CGRectMake((CGRectGetWidth(recordBgView.frame) - 120) / 2.0, 30, 120, 80);
            
            UILabel *label = [[UILabel alloc] init];
            label.font = kFont_13;
            label.textColor = self.textColor;
            label.text = @"还没有邻居中奖喔！赶紧去试试吧！";
            label.textAlignment = NSTextAlignmentCenter;
            [recordBgView addSubview:label];
            label.frame = CGRectMake(0, CGRectGetMaxY(imgView.frame) + 9, CGRectGetWidth(recordBgView.frame), 15);
            
            self.bottomLabel.frame = CGRectMake((LYScreenWidth() - CGRectGetWidth(self.bottomLabel.bounds)) / 2.0, CGRectGetMaxY(recordBgView.frame) + 45, CGRectGetWidth(self.bottomLabel.bounds), 15);
        }else {
            NSUInteger row = MIN(self.lotteryDetailModel.lotteryUsers.count, 3);
            CGFloat winRecordViewHeight = kTriggerTextFont * row + kTriggerPadding * (row + 1);
            
            recordBgView.frame = CGRectMake(15, CGRectGetMaxY(numLabel.frame) + 18, LYScreenWidth() - 30, winRecordViewHeight + 10);
            
            SEGWinReocordViewType showType = SEGWinReocordViewType_Trigger;
            if (self.lotteryDetailModel.lotteryType.integerValue == 3) {
                // 抽奖活动颜色展示黄色
                showType = SEGWinReocordViewType_RedPackage;
            }
            
            if (!self.winRecordView) {
                self.winRecordView = [[SEGActivityWinRecordView alloc] initWithFrame:CGRectMake(0, 5, CGRectGetWidth(recordBgView.frame), winRecordViewHeight) type:showType dataSouce:self.lotteryDetailModel.lotteryUsers];
            } else {
                // 如果数据有刷新 就重新赋值轮播视图
                BOOL __block needResetWinRecordView = NO;
                NSArray<SEGActivityLotteryUsersModel *> *oldLotterys = self.winRecordView.lotteryModelArray;
                NSArray<SEGActivityLotteryUsersModel *> *newLotterys = self.lotteryDetailModel.lotteryUsers;
                if (oldLotterys.count != newLotterys.count) {
                    needResetWinRecordView = YES;
                } else {
                    [oldLotterys enumerateObjectsUsingBlock:^(SEGActivityLotteryUsersModel * _Nonnull oldObj, NSUInteger idx, BOOL * _Nonnull stop) {
                        SEGActivityLotteryUsersModel *newObj = newLotterys[idx];
                        // 有一项值不一致就重新加载
                        if (![oldObj.lotteryTime isEqualToString:newObj.lotteryTime] || ![oldObj.nickName isEqualToString:newObj.nickName] || ![oldObj.prizeName isEqualToString:newObj.prizeName]) {
                            needResetWinRecordView = YES;
                            *stop = YES;
                        }
                    }];
                }
                
                // 重新加载轮播视图
                if (needResetWinRecordView) {
                    [self.winRecordView removeFromSuperview];
                    self.winRecordView = nil;
                    self.winRecordView = [[SEGActivityWinRecordView alloc] initWithFrame:CGRectMake(0, 44.0, LYScreenWidth(), winRecordViewHeight) type:SEGWinReocordViewType_Lottery dataSouce:self.lotteryDetailModel.lotteryUsers];
                }
            }
            [recordBgView addSubview:self.winRecordView];
            
            self.bottomLabel.frame = CGRectMake((LYScreenWidth() - CGRectGetWidth(self.bottomLabel.bounds)) / 2.0, CGRectGetMaxY(recordBgView.frame) + 45, CGRectGetWidth(self.bottomLabel.bounds), 15);
        }
    }
    
    CGFloat lineMargin = 10;
    UIView *leftLine = [[UIView alloc] init];
    leftLine.backgroundColor = self.bottomLabel.textColor;
    [self.scrollView addSubview:leftLine];
    leftLine.frame = CGRectMake(CGRectGetMinX(self.bottomLabel.frame) - 45.0 - lineMargin, CGRectGetMidY(self.bottomLabel.frame) - 0.5, 45.0, 0.5);
    
    UIView *rightLine = [[UIView alloc] init];
    rightLine.backgroundColor = self.bottomLabel.textColor;
    [self.scrollView addSubview:rightLine];
    rightLine.frame = CGRectMake(CGRectGetMaxX(self.bottomLabel.frame) + lineMargin, CGRectGetMidY(self.bottomLabel.frame) - 0.5, 45.0, 0.5);
    
    self.scrollView.contentSize = CGSizeMake(LYScreenWidth(), CGRectGetMaxY(self.bottomLabel.frame) + 13);
}

- (void)seg_beginLottery
{
    [SVProgressHUD show];
    
    
    [[self.detailViewModel.triggerLotteryCommand execute:RACTuplePack(self.actId, self.triggerEventId, self.lotteryId)] subscribeNext:^(id  _Nullable x) {
        [SVProgressHUD dismiss];
        RACTupleUnpack(NSNumber *isSuccessNumber, NSString *errorMessage, SEGActivityLotteryResultModel *lotteryResultModel) = x;
        BOOL isSuccessed = isSuccessNumber.boolValue;
        self.lotteryResultModel = lotteryResultModel;
        
        if (isSuccessed) {//减少一次抽奖
            [self seg_removeCurrentLotteryInfoInLocalArray];
        }
        self.isLotterySucceed = isSuccessed;
        self.lotteryErrorMessage = errorMessage;
        
        SEGActivityLotteryType lotteryType = [self.lotteryDetailModel.lotteryType intValue];
        if (lotteryType == SEGActivityLottery_RedWallet) {//红包
            [self seg_showLotteryResult];
        }else {
            [self.topImageView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.tag == 1001) {//大转盘
                    SEGActivityLotteryRotateView *view = (SEGActivityLotteryRotateView *)obj;
                    [view startAnimation:self.lotteryResultModel.prizeId];
                    *stop = YES;
                }else if (obj.tag == 1003) {//砸金蛋
                    SEGActivityLotteryBreakEggsView *view = (SEGActivityLotteryBreakEggsView *)obj;
                    BOOL hasWin = self.lotteryResultModel.notLotteryMsg.length > 0 ? NO : YES;//是否中奖
                    [view startAnimatin:hasWin eggIndex:self.eggIndex];
                    //延迟弹出结果
                    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC));
                    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                        [self seg_showLotteryResult];
                    });
                }
            }];
        }
    }];
}

- (void)seg_showLotteryResult
{
    if (self.isLotterySucceed) {
        
        SEGActivityLotteryResultModel *resultModel = self.lotteryResultModel;
        SEGAlerViewHUDType hudType;//弹窗类型
        BOOL hasWin = resultModel.notLotteryMsg.length > 0 ? NO : YES;//是否中奖
        NSString *picUrl = hasWin ? resultModel.prizePic : resultModel.notLotteryPic;
        
        //中奖结果弹窗
        SEGActivityLotteryType lotteryType = [self.lotteryDetailModel.lotteryType intValue];
        switch (lotteryType) {
            case SEGActivityLottery_BreakEggs://砸金蛋
            {
                if (hasWin) {
                    hudType = SEGAlerViewHUDType_EggWin;
                }else {
                    hudType = SEGAlerViewHUDType_EggLose;
                }
                break;
            }
            case SEGActivityLottery_Turntable://大转盘
            {
                if (hasWin) {
                    hudType = SEGAlerViewHUDType_TurnTableWin;
                }else {
                    hudType = SEGAlerViewHUDType_TurnTableLose;
                }
                break;
            }
            case SEGActivityLottery_RedWallet://红包
            {
                if (hasWin) {
                    hudType = SEGAlerViewHUDType_NarmalWin;
                }else {
                    hudType = SEGAlerViewHUDType_NarmalLose;
                }
                break;
            }
        }
        
        [SEGAlerViewHUD show:hudType prizeName:resultModel.prizeName picUrl:picUrl trytime:-100 notLotteryMsg:resultModel.notLotteryMsg finished:^(SEGAlerViewHUDResult type) {
            //处理弹窗按钮回调
            switch (type) {
                case SEGAlerViewHUDResult_Know:/*我知道了*/
                {
                    [self.navigationController popViewControllerAnimated:YES];
                    break;
                }
                case SEGAlerViewHUDResult_ShowPrize:/*查看奖励*/
                {
                    [self seg_goToWinRecordVC];
                    break;
                }
                case SEGAlerViewHUDResult_TryAgin:/*再来一次*/
                {
                    [self.navigationController popViewControllerAnimated:YES];
                    break;
                }
                case SEGAlerViewHUDResult_WalletHistory:/*中奖记录*/
                {
                    [self seg_goToWinRecordVC];
                    break;
                }
                case SEGAlerViewHUDResult_LastTime:/*您的抽奖机会已用完！*/
                {
                    [self.navigationController popViewControllerAnimated:YES];
                    break;
                }
                case SEGAlerViewHUDResult_Close:
                {
                    [self.navigationController popViewControllerAnimated:YES];
                    break;
                }
            }
        }];
    }else {
        [SEGAlertView showTmepMessageWithMessage:self.lotteryErrorMessage];
    }
}

- (void)seg_winRecordButtonPressed
{
    SEGWinRecordListViewController* winRecordList = [[SEGWinRecordListViewController alloc] init];
    [self.navigationController pushViewController:winRecordList animated:YES];
}

- (void)seg_goToWinRecordVC
{
    // 中奖详情
    SEGWinRecordDetailViewController *detailVC = [[SEGWinRecordDetailViewController alloc] initWithWinnerId:self.lotteryResultModel.winnerId];
    // 防止重复返回抽奖
    NSMutableArray *vcArr = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [vcArr removeObject:self];
    [vcArr addObject:detailVC];
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController setViewControllers:vcArr animated:YES];
    //    // 中奖纪录列表
    //    SEGWinRecordListViewController* winRecordList = [[SEGWinRecordListViewController alloc] init];
    //    winRecordList.shouldPassTriggerLotteryView = YES;
    //    [self.navigationController pushViewController:winRecordList animated:YES];
    
}

// 移除当前中奖机会
- (void)seg_removeCurrentLotteryInfoInLocalArray
{
    //获取本地抽奖信息
    NSString *key = [NSString stringWithFormat:@"%@_%@",kActivityLotteryInfoArrayNew, kAccountUserID];
    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    if(!array || array.count == 0) {//新路径获取不到，获取老数据
        key = kActivityLotteryInfoArray;
        array = [[NSUserDefaults standardUserDefaults] objectForKey:kActivityLotteryInfoArray];
    }
    NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:array];
    if (mutableArray.count > 0) {
        [mutableArray removeObjectAtIndex:0];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:mutableArray forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:SEGActivityLotteryNotification object:nil];
}

#pragma mark - getter
- (UIImageView *)topImageView
{
    if (!_topImageView) {
        _topImageView = [[UIImageView alloc] init];
        [self.scrollView addSubview:_topImageView];
        _topImageView.userInteractionEnabled = YES;
    }
    return _topImageView;
}

- (SEGAdjustableButton *)winRecordButton
{
    if (!_winRecordButton) {
        _winRecordButton = [[SEGAdjustableButton alloc] init];
        NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"我的中奖记录 >"];
        NSRange strRange2 = {0,[str2 length]};
        [str2 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange2];
        [str2 addAttribute:NSForegroundColorAttributeName value:self.textColor range:strRange2];
        [_winRecordButton setAttributedTitle:str2 forState:UIControlStateNormal];
        
        [_winRecordButton setTitleColor:kColor_S1 forState:UIControlStateNormal];
        _winRecordButton.titleLabel.font = kFont_13;
        [_winRecordButton addTarget:self action:@selector(seg_winRecordButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:_winRecordButton];
        [_winRecordButton layoutButtonWithEdgeInsetsStyle:SEGButtonEdgeInsetsStyleLeft imageTitleSpace:6];
    }
    return _winRecordButton;
}

- (UILabel *)newsLabel
{
    if (!_newsLabel) {
        _newsLabel = [[UILabel alloc] init];
        _newsLabel.font = kFont_13;
        _newsLabel.text = @"最新动态";
        _newsLabel.textColor = self.textColor;
        _newsLabel.textAlignment = NSTextAlignmentCenter;
        [self.scrollView addSubview:_newsLabel];
    }
    return _newsLabel;
}

- (UIImageView *)newsLeftImage
{
    if (!_newsLeftImage) {
        _newsLeftImage = [[UIImageView alloc] init];
        [self.scrollView addSubview:_newsLeftImage];
    }
    return _newsLeftImage;
}

- (UIImageView *)newsRightImage
{
    if (!_newsRightImage) {
        _newsRightImage = [[UIImageView alloc] init];
        [self.scrollView addSubview:_newsRightImage];
    }
    return _newsRightImage;
}

- (UILabel *)bottomLabel
{
    if (!_bottomLabel) {
        _bottomLabel = [[UILabel alloc] init];
        _bottomLabel.font = kFont_13;
        _bottomLabel.text = kAppName;
        _bottomLabel.textAlignment =NSTextAlignmentCenter;
        [_bottomLabel sizeToFit];
        [self.scrollView addSubview:_bottomLabel];
    }
    return _bottomLabel;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.bounces = NO;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

- (SEGActivityDetailViewModel *)detailViewModel {
    if (!_detailViewModel) {
        _detailViewModel = [[SEGActivityDetailViewModel alloc] init];
    }
    return _detailViewModel;
}

@end

